package model;


import com.ibm.staf.STAFHandle;
import com.ibm.staf.STAFResult;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author pyin
 */
public class Tester {

    public static void main3(String[] args){
        try {
//            File pwd = new File("./template/smoke.xml");
//            if(pwd.exists())
//                System.out.println(pwd.getCanonicalFile());
//            else
//                System.out.println("file not exist");
            Boolean obj = Boolean.valueOf(true);
            

            TestScript ts = new TestScript("Script2",ConfigManager.getServerHostName(),"buildName");

//            System.out.println(ts.getScriptPath());
//            System.out.println(toUnixPattern(ts.getScriptPath()));
            ts.run();
        } catch (Exception ex) {
            Logger.getLogger(Tester.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    private static String toUnixPattern(String pattern){
        return pattern.replace("\\", "/");
    }

    private static String wrapWithQuote(String stupidPathWithBlack){
        stupidPathWithBlack = stupidPathWithBlack.replace("\\", "\\\\");
		stupidPathWithBlack = stupidPathWithBlack.replace(".", "\\.");
		return "\\\""+stupidPathWithBlack+"\\\"";
    }

    private static String wrapWithQuote2(String stupidPathWithBlack){
        stupidPathWithBlack = stupidPathWithBlack.replace("\\", "\\\\");
		stupidPathWithBlack = stupidPathWithBlack.replace(".", "\\.");
		return "\""+stupidPathWithBlack+"\"";
    }

    public static void main2(String[] args){

		STAFResult result;
        STAFHandle handle;

		try
		{
			handle = new STAFHandle("STAXJobLister");

//			result = handle.submit2("local", "rft", "runscript \"C:\\\\Documents and Settings\\\\\dongxu\\\\IBM\\\\rationalsdp7.0\\\\workspace\\\\UEPInstall\\\\Script1\" wait returnresult");

            String xml = "C:\\Documents and Settings\\dongxu\\My Documents\\NetBeansProjects\\test\\template\\rft.xml";
            String xml2 = "C:/Documents and Settings/dongxu/My Documents/NetBeansProjects/test/template/rft.xml";
            result = handle.submit2("local","stax","execute file "+wrapWithQuote(xml)+" wait returnresult");
//            result = handle.submit2("local","stax","execute file "+wrapWithQuote2(xml2)+" wait returnresult");
			if (result.rc==0){
                System.out.println("TS launched successfully");
			}else{
                System.out.println(result.result);
			}

		}catch(Exception e){

		}
    }

    public static void main(String[] args){
        List machines = VMMachineFactory.getVMMachines();
        Iterator it = machines.iterator();
        while(it.hasNext()){
            try {
                VMMachine machine = (VMMachine) it.next();
                if (machine.isStarted() && machine.getRunningScript() == null) {
                    System.out.println(machine.getName());
                }
            } catch (VMNotReadyException ex) {
                Logger.getLogger(Tester.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InvalidTSException ex) {
                Logger.getLogger(Tester.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InvalidVMHostNameException ex) {
                Logger.getLogger(Tester.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

}
